import Map from 'ol/Map'
import VectorLayer from 'ol/layer/Vector'
import VectorSource from 'ol/source/Vector'
import GeoJSON from "ol/format/GeoJSON"
/**
 * 图层管理类
 */
export default class LayerManager {
    // 用于临时存储图层信息
    private static layers: Array<any> = []
    private static map: Map
    constructor(map: Map) {
        LayerManager.map = map
    }

    /**
     * 添加图层
     * @param vectorLayer 图层源
     * @param layerId 图层标识
     */
    public addLayer(vectorLayer: VectorLayer, layerId: string, isAllowDelete: boolean) {
        if (this.getLayerById(layerId) !== null) {
            console.warn("图层已存在，请勿重复添加！")
            return
        }
        LayerManager.layers.push({ id: layerId, vectorLayer: vectorLayer, isAllowDelete })
        vectorLayer.set("id", layerId)
        LayerManager.map.addLayer(vectorLayer)

    }

    /**
     * 移除图层
     * @param layerId 图层id
     */
    public removeLayer(layerId: string) {

        const layer = this.getLayerById(layerId)
        if (layer !== null) {
            try {
                let vectorSource = layer.getSource() as VectorSource
                if (vectorSource != null) {
                    vectorSource.clear()// 清除现有特征元素
                }
                LayerManager.map.removeLayer(layer) // 清除图层

                // 删除本地缓存数据
                const deleteIndex = LayerManager.layers.findIndex(m => m.id == layerId)
                LayerManager.layers.splice(deleteIndex, 1)
            } catch (error) {
                LayerManager.map.removeLayer(layer) // 清除图层

                // 删除本地缓存数据
                const deleteIndex = LayerManager.layers.findIndex(m => m.id == layerId)
                LayerManager.layers.splice(deleteIndex, 1)
            }



        }
    }
    /**
     * 移除除区域底层外的全部图层
     */
    public removeAllLayer() {
        const layers = this.getTemporaryLayer()
        layers.forEach(layer => {
            if (layer.isAllowDelete) {
                this.removeLayer(layer.id)
            }
        })
    }

    public setVisble(isShow: boolean, layerId: string) {
        const layer = this.getLayerById(layerId)
        if (layer != null) {
            layer.setVisible(isShow)
        }
    }
    /**
     * 防止深拷贝，可以预先获取临时全部图层
     * @returns 临时全部的图层
     */
    private getTemporaryLayer(): Array<any> {
        let layers: Array<any> = []
        LayerManager.layers.forEach(layer => {
            layers.push(layer)
        })
        return layers
    }

    /**
     * 更改图层信息
     * @param layerId 图层id
     * @param newSource 新数据源
     * @param newStyle 新样式
     */
    public updateLayer(layerId: string, newSource: any, newStyle?: any) {
        const layer = this.getLayerById(layerId)
        if (layer !== null) {
            let vectorSource = layer.getSource() as VectorSource
            vectorSource.clear()// 清除现有特征元素
            // 添加新特征
            vectorSource.addFeatures(new GeoJSON().readFeatures(newSource, {
                featureProjection: "EPSG:3857", // 目标投影是WEB Mercator
                dataProjection: "EPSG:4326",  // 源数据投影是WGS 84
            }))
            if (newStyle) {
                layer.setStyle(newStyle) // 更新样式
            }
        }
    }

    /**
     *  根据图层id获取图层实例
     * @param layerId layer图层标识
     * @returns  返回一个图层实例
     */
    private getLayerById(layerId: string): VectorLayer {
        const layer_index = LayerManager.layers.findIndex(m => m.id == layerId)
        let layer_vector
        if (layer_index != -1) {
            layer_vector = LayerManager.layers[layer_index].vectorLayer
        } else {
            layer_vector = null
        }
        return layer_vector
    }


}